package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.dictionary.pojo.DictionaryPojo;
import com.xbongbong.pro.dictionary.pojo.dto.DictionaryAddDTO;
import com.xbongbong.pro.dictionary.pojo.dto.DictionaryListDTO;
import com.xbongbong.pro.dictionary.pojo.vo.DictionaryVO;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.DataDictionaryErrorCodeEnum;
import com.xbongbong.saas.domain.entity.DataDictionaryEntity;
import com.xbongbong.saas.enums.dictionary.DictionaryEnum;
import com.xbongbong.saas.model.DataDictionaryModel;
import com.xbongbong.saas.service.DictionaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/1/31 9:03
 * @since v1.0
 */
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {

    @Resource
    private DataDictionaryModel dataDictionaryModel;

    @Override
    public BaseVO add(DictionaryAddDTO dictionaryAddDTO) throws XbbException {
        DataDictionaryEntity dbDictionary = dataDictionaryModel.getByKey(dictionaryAddDTO.getId(),dictionaryAddDTO.getCorpid());
        List<DictionaryPojo> dbList = JSONArray.parseArray(dbDictionary.getData(),DictionaryPojo.class);
        Integer maxCode = 0;
        for (DictionaryPojo pojo : dbList){
            Integer code = Integer.valueOf(pojo.getValue());
            maxCode = code > maxCode ? code : maxCode;
        }
        List<DictionaryPojo> list =  dictionaryAddDTO.getList();
        Set<String> nameSet = new HashSet<>();
        Set<String> codeSet = new HashSet<>();
        for (DictionaryPojo pojo : list){
            if (pojo.getValue() == null){
                pojo.setValue((++maxCode).toString());
            }
            nameSet.add(pojo.getText());
            codeSet.add(pojo.getValue());
        }
        if (nameSet.size() < list.size() || codeSet.size() < list.size()){
            throw new XbbException(DataDictionaryErrorCodeEnum.API_ERROR_370001);
        }
        dataDictionaryModel.update(dictionaryAddDTO.getId(),dictionaryAddDTO.getCorpid(),JSONObject.toJSONString(list));
        dataDictionaryModel.removeRedis(dictionaryAddDTO.getCorpid(),dictionaryAddDTO.getType());
        return new BaseVO();
    }

    @Override
    public DictionaryVO list(DictionaryListDTO dictionaryListDTO) throws XbbException {
        return dataDictionaryModel.list(dictionaryListDTO);
    }

    @Override
    public List<ItemPoJo> getArrayList(DictionaryEnum dictionaryEnum, String corpid, Long formId) throws XbbException {
        //只获取启用的，且不限制addtionnalField
        return dataDictionaryModel.getArrayList(dictionaryEnum, corpid, formId);
    }

    @Override
    public Map<String, String> getDictionaryMap(DictionaryEnum dictionaryEnum, String corpid, Long formId) throws XbbException {
        return dataDictionaryModel.getDictionaryMap(dictionaryEnum, corpid, formId);
    }
}
